package org.android.kumoh_ing;
 

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
 
 public class InputCalendarActivity extends Activity {
	
	 int startYear,startMonth,startDay,startHour,startMin;
	 int endYear,endMonth,endDay,endHour,endMin;
	 String startformat;
	 String endformat;
	 String summaryvalue;
	 String Date,Time;
	 Button StartBtnDate;
	 Button StartBtnTime;
	 Button EndBtnDate;
	 Button EndBtnTime;
	 Button btnOK;
	 Button btnCancel;
	 EditText summary;
	 
	 Calendar oCalendar = Calendar.getInstance( );  // 占쏙옙占쏙옙 占쏙옙짜/占시곤옙 占쏙옙占쏙옙 占쏙옙占쏙옙 占쏙옙占쏙옙 占쏙옙占�	 
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.newevent);
       init();
     }
     
     public void init(){
    	 
     	int year= oCalendar.get(Calendar.YEAR);
     	int month=oCalendar.get(Calendar.MONTH) + 1;
     	int day=oCalendar.get(Calendar.DAY_OF_MONTH);
     	int hour= oCalendar.get(Calendar.HOUR_OF_DAY); // 24占시곤옙占쏙옙
     	int minute = oCalendar.get(Calendar.MINUTE);
     	
     	StartBtnDate = (Button)findViewById(R.id.startBtnDate);
     	StartBtnTime = (Button)findViewById(R.id.startBtnTime);
     	EndBtnDate = (Button)findViewById(R.id.endBtnDate);
     	EndBtnTime = (Button)findViewById(R.id.endBtnTime);
     	btnOK=(Button)findViewById(R.id.btnOK);
     	btnCancel=(Button)findViewById(R.id.btnCancel);
     	summary = (EditText)findViewById(R.id.CalSummary);
     	
     	Date= year + "년"+ month +"월" +day +"일";
  		Time = hour+"시"+minute+"분";
  		StartBtnDate.setText(Date);
  		StartBtnTime.setText(Time);
  		EndBtnDate.setText(Date);
  		EndBtnTime.setText(Time);
  		
  		startYear = year;
  		startMonth = month;
  		startDay = day;
  		endYear= year;
  		endMonth = month;
  		endDay = day;
  		startHour = hour;
  		startMin = minute;
  		endHour = hour;
  		endMin = minute;
    	 
     	
 		 
     	 
 			StartBtnDate.setOnClickListener(new OnClickListener(){
 				public void onClick(View v){
 					 new DatePickerDialog(InputCalendarActivity.this,
 							  mDateSetListener, 
 							  oCalendar.get(Calendar.YEAR),
 							  oCalendar.get(Calendar.MONTH),
 							  oCalendar.get(Calendar.DAY_OF_MONTH)).show(); 

 					
 				}
 			});
 			StartBtnTime.setOnClickListener(new OnClickListener(){
 				public void onClick(View v){
 					 new TimePickerDialog(InputCalendarActivity.this,
 							  mTimeSetListener, 
 							  oCalendar.get(Calendar.HOUR_OF_DAY),
 							  oCalendar.get(Calendar.MINUTE),
 							  true).show(); 

 					
 					
 				}
 			});
 			EndBtnDate.setOnClickListener(new OnClickListener(){
 				public void onClick(View v){
 					 new DatePickerDialog(InputCalendarActivity.this,
 							  mDateSetListener1, 
 							  oCalendar.get(Calendar.YEAR),
 							  oCalendar.get(Calendar.MONTH),
 							  oCalendar.get(Calendar.DAY_OF_MONTH)).show(); 

 					
 					
 				}
 			});
 			EndBtnTime.setOnClickListener(new OnClickListener(){
 				public void onClick(View v){
 					 new TimePickerDialog(InputCalendarActivity.this,
 							  mTimeSetListener1, 
 							  oCalendar.get(Calendar.HOUR_OF_DAY),
 							  oCalendar.get(Calendar.MINUTE),
 							  true).show(); 
 					
 					
 				}
 			});
 			
 			btnOK.setOnClickListener(new OnClickListener(){
 				public void onClick(View v){
 					startformat = startYear+"-"+underTen(startMonth)+"-"+underTen(startDay)+"T"+underTen(startHour)+":"+underTen(startMin)+":00.000+09:00";	
 		 			endformat = endYear+"-"+underTen(endMonth)+"-"+underTen(endDay)+"T"+underTen(endHour)+":"+underTen(endMin)+":00.000+09:00";				
 					 Intent resultIntent = new Intent();
 					 summaryvalue = summary.getText().toString();
 					
 	        		 resultIntent.putExtra("startformat",startformat);
 	        		 resultIntent.putExtra("endformat",endformat);
 	        		 resultIntent.putExtra("summaryvalue",summaryvalue);
 	        		 
 	        		 
 	        		 setResult(RESULT_OK,resultIntent);
 	        		 finish();
 	        		  
 				}	
 			});
 			btnCancel.setOnClickListener(new OnClickListener(){
 				public void onClick(View v){
 					Intent resultIntent = new Intent();
 					
 					setResult(RESULT_CANCELED,resultIntent);
 	        		finish();
 				}
 			});
 		
 			
      }
     
     public void setStartDate(){
    	 Date= startYear + "년"+ startMonth +"월" +startDay +"일";
    	 StartBtnDate.setText(Date);
     }
     public void setEndDate(){
    	 Date= endYear + "년"+ endMonth +"월" +endDay +"일";
    	 EndBtnDate.setText(Date);
     }
     public void setStartTime(){
    	 Time = startHour+"시"+startMin+"분";
    	 StartBtnTime.setText(Time);
    	 
     }
     public void setEndTime(){
    
    	 Time = endHour+"시"+endMin+"분";
    	 EndBtnTime.setText(Time);
     }
     
    
     public String underTen(int num){
    	 if(num<10)
    		 return "0"+num;
    	 else
    		 return ""+num;
    			 
     }

     
     private DatePickerDialog.OnDateSetListener mDateSetListener =new DatePickerDialog.OnDateSetListener()
     {
   	
   	@Override
   	public void onDateSet(DatePicker view, int year, int monthOfYear,
   			int dayOfMonth) {
   		// TODO Auto-generated method stub
   		oCalendar.set(year, monthOfYear, dayOfMonth);
   		startYear =year;
   		startMonth=monthOfYear+1;
   		startDay=dayOfMonth;
   		setStartDate();
   		
   	}
   };
   private DatePickerDialog.OnDateSetListener mDateSetListener1 =new DatePickerDialog.OnDateSetListener()
   {
 	
 	@Override
 	public void onDateSet(DatePicker view, int year, int monthOfYear,
 			int dayOfMonth) {
 		// TODO Auto-generated method stub
 		oCalendar.set(year, monthOfYear, dayOfMonth);
 		endYear =year;
 		endMonth=monthOfYear+1;
 		endDay=dayOfMonth;
 		setEndDate();
 		
 	}
 };

   private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
   	
   	@Override
   	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
   		// TODO Auto-generated method stub

   		oCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
   		oCalendar.set(Calendar.MINUTE, minute);

   		startHour = hourOfDay;
   		startMin = minute;
   		setStartTime();
   		
   	}
   };
   private TimePickerDialog.OnTimeSetListener mTimeSetListener1 = new TimePickerDialog.OnTimeSetListener() {
	   	
	   	@Override
	   	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
	   		// TODO Auto-generated method stub

	   		oCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
	   		oCalendar.set(Calendar.MINUTE, minute);

	   		endHour = hourOfDay;
	   		endMin = minute;
	   		setEndTime();
	   		
	   	}
	   };
   
  
 
 }
 